
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;

namespace BlazeApps.Library.Editor
{
    [Serializable()]
    public class EditorChild : Csla.ReadOnlyBase<EditorChild>
    {
        #region Business Properties and Methods

        //declare members
        private Guid _Templateid = Guid.NewGuid();
        private string _TemplateName = string.Empty;
        private int _NumberOfPlaceholders = 0;
        private int _PlaceholderNumber = 0;
        private string _FieldName = string.Empty;
        private int _FieldType = 0;
        private int _FieldHeight = 0;
        private int _FieldWidth = 0;
        private string _PlaceholderData = string.Empty;

        [System.ComponentModel.DataObjectField(true, true)]
        public Guid Templateid
        {
            get
            {
                CanReadProperty("Templateid", true);
                return _Templateid;
            }
        }

        public string TemplateName
        {
            get
            {
                CanReadProperty("TemplateName", true);
                return _TemplateName;
            }
        }

        public int NumberOfPlaceholders
        {
            get
            {
                CanReadProperty("NumberOfPlaceholders", true);
                return _NumberOfPlaceholders;
            }
        }

        public int PlaceholderNumber
        {
            get
            {
                CanReadProperty("PlaceholderNumber", true);
                return _PlaceholderNumber;
            }
        }

        public string FieldName
        {
            get
            {
                CanReadProperty("FieldName", true);
                return _FieldName;
            }
        }

        public int FieldType
        {
            get
            {
                CanReadProperty("FieldType", true);
                return _FieldType;
            }
        }

        public int FieldHeight
        {
            get
            {
                CanReadProperty("FieldHeight", true);
                return _FieldHeight;
            }
        }

        public int FieldWidth
        {
            get
            {
                CanReadProperty("FieldWidth", true);
                return _FieldWidth;
            }
        }

        public string PlaceholderData
        {
            get
            {
                CanReadProperty("PlaceholderData", true);
                return _PlaceholderData;
            }
        }

        protected override object GetIdValue()
        {
            return _Templateid;
        }

        #endregion //Business Properties and Methods

        #region Authorization Rules
        protected override void AddAuthorizationRules()
        {
            //TODO: Define authorization rules in EditorChild
            //AuthorizationRules.AllowRead("templateid", "EditorChildReadGroup");
            //AuthorizationRules.AllowRead("templatename", "EditorChildReadGroup");
            //AuthorizationRules.AllowRead("numberofplaceholders", "EditorChildReadGroup");
            //AuthorizationRules.AllowRead("placeholdernumber", "EditorChildReadGroup");
            //AuthorizationRules.AllowRead("fieldname", "EditorChildReadGroup");

        }

        #endregion //Authorization Rules

        #region Factory Methods
        internal static EditorChild GetEditorChild(SafeDataReader dr)
        {
            return new EditorChild(dr);
        }

        private EditorChild(SafeDataReader dr)
        {
            Fetch(dr);
        }
        #endregion //Factory Methods

        #region Data Access

        #region Data Access - Fetch
        private void Fetch(SafeDataReader dr)
        {
            FetchObject(dr);

            //load child object(s)
            FetchChildren(dr);
        }

        private void FetchObject(SafeDataReader dr)
        {
            _TemplateName = dr.GetString("templatename");
            _NumberOfPlaceholders = dr.GetInt32("numberofplaceholders");
            _PlaceholderNumber = dr.GetInt32("placeholdernumber");
            _FieldName = dr.GetString("fieldname");
            _FieldType = dr.GetInt32("fieldtype");
            _FieldHeight = dr.GetInt32("fieldheight");
            _FieldWidth = dr.GetInt32("fieldwidth");
            _PlaceholderData = dr.GetString("placeholderdata");
        }

        private void FetchChildren(SafeDataReader dr)
        {
        }
        #endregion //Data Access - Fetch

        #endregion //Data Access
    }
}
